import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

Vue.use(VueRouter)

// 一级路由配置
const Login = () => import('../pages/Login/Login.vue')
const Index = () => import('../pages/Index/Index.vue')

// 二级路由配置
const Home = () => import('../views/Home/Home.vue')
const Menu = () => import('../views/Menu/Menu.vue')
const Role = () => import('../views/Role/Role.vue')
const User = () => import('../views/User/User.vue')
const Cate = () => import('../views/Cate/Cate.vue')
const Specs = () => import('../views/Specs/Specs.vue')
const Management = () => import ('../views/Management/Management.vue')
const Member = () => import ('../views/Member/Member.vue')
const Banner = () => import ('../views/Banner/Banner.vue')
const Seck = () => import ('../views/Seck/Seck.vue')


function foo(name){
  return store.state.user.menus_url.some(item=>item===name)
 }

const routes = [
  {
    path: '/login',
    component: Login,
    name: '登录',
    meta: { title: '登录' },
  },
  {
    path: '/index',
    component: Index,
    name: '首页',
    meta: { title: '首页' },
    children: [
      {
        path: 'home',
        component: Home,
        name: '首页-主页面',
        meta: { title: '首页-主页面' },
      },
      {
        path: 'menu',
        component: Menu,
        name: '菜单',
        meta: { title: '菜单' },
        beforeEnter(to,from,next){
          foo('menu')? next() : next('/index')
       }
      },
      {
        path: 'role',
        component: Role,
        name: '角色管理',
        meta: { title: '角色管理' },
        beforeEnter(to,from,next){
          foo('role') ? next() : next('/index')
        }
      },
      {
        path: 'manager',
        component: User,
        name: '管理员管理',
        meta: { title: '管理员管理' },
        beforeEnter(to,from,next){
          foo('manager') ? next() : next('/index')
        }
      },
      {
        path: 'cate',
        component: Cate,
        name: '商品分类',
        meta: { title: '商品分类' },
        beforeEnter(to,from,next){
          foo('cate') ? next() : next('/index')
        }
      },
      {
        path: 'specs',
        component: Specs,
        name: '商品规格',
        meta: { title: '商品规格' },
        beforeEnter(to,from,next){
          foo('specs') ? next() : next('/index')
        }
      },
      {
        path:'management',
        component:Management,
        name:'商品管理',
        meta:{title:'商品管理'},
        beforeEnter(to,from,next){
          foo('management') ? next() : next('/index')
        }
      },
      {
        path:'member',
        component:Member,
        name:'会员管理',
        meta:{title:'会员管理'},
        beforeEnter(to,from,next){
          foo('member') ? next() : next('/index')
        }
      },
      {
        path:'banner',
        component:Banner,
        name:'轮播图管理',
        meta:{title:'轮播图管理'},
        beforeEnter(to,from,next){
          foo('banner') ? next() : next('/index')
        }
      },
      {
        path:'seck',
        component:Seck,
        name:'秒杀管理',
        meta:{title:'秒杀管理'},
        beforeEnter(to,from,next){
          foo('seck') ? next() : next('/index')
        }
      },
      {
        path: '/index',
        redirect:'/index/home'
      }

    ]
  },
  {
    path: '*',
    redirect: '/login'
  }
]

const router = new VueRouter({
  routes
})

export default router
